<?php
namespace app\wechat\controller;
use app\models\Bases;

class Sign extends Base{
    /**
     * @return \think\response\Json
     * 签到的积分接口
     */
    public function signAdd(){
        try{
            $user = new Bases('user');
            $uid  = checkLogin();
            if(empty($uid))
            throw new \LogicException('签到请先登录',10010);
            $userInfo  = self::getUserInfo("uid = $uid");
            if($userInfo['type']==3){ 
                $check_in_time = strtotime(date('Y-m-d',time())); 
                if($check_in_time  ==  $userInfo['check_in_time']){
                    throw new \LogicException('请不要重复签到',10020); 
                }
                if(!$user->setField("uid = $uid",['check_in_time'=>$check_in_time])){
                    throw new \LogicException('签到失败！',10030);
                }
                if(!$user->_setInc("uid = $uid","integral",5)){
                    throw new \LogicException('操作失败！',10040);
                }
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }
    /**
     * @return \think\response\Json
     * 领取牛股减少积分接口
     */
    public function signDelete(){
        try{
            $user = new Bases('user');
            $uid  = checkLogin();
            if(empty($uid))
                throw new \LogicException('查看牛股请先登录',10010);
            $userInfo  = self::getUserInfo("uid = $uid");
            if($userInfo['integral']<5)
                throw new \LogicException('您的积分不够，请联系客服',10020);
            if($userInfo['type']==3){ 
                if(!$user->_setDec("uid = $uid","integral",5)){
                    throw new \LogicException('操作失败',10030);
                }
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult());
    }
}